//
//  MoreHouseTypeView.m
//  YiJuZhangShou2.0
//
//  Created by URANUS on 16/3/27.
//  Copyright © 2016年 Mr.X. All rights reserved.
//

#import "MoreHouseTypeView.h"
#import "ContainerView.h"
#import "Masonry.h"
#import "MoreHouseTypeCollectionCell.h"
#import "MoreHouseTypeModel.h"

static NSString *MoreHouseTypeCollectionCellIdentifier = @"MoreHouseTypeCollectionCell";

@interface MoreHouseTypeView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation MoreHouseTypeView
#pragma mark - Initialize
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self loadMoreHouseTypePage];
    }
    return self;
}

#pragma mark Delegate UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (_models != nil) {
        
        return _models.count;
    }else{
        return 0;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MoreHouseTypeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:MoreHouseTypeCollectionCellIdentifier forIndexPath:indexPath];
    MoreHouseTypeModel *model = [_models objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

#pragma mark Delegate UICollectionViewDelegateFlowLayout
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 25.0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10.0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.frame.size.width/3-10,self.frame.size.width/3-10);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

#pragma mark Delegate UICollectionViewDelegate
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    DDLogInfo(@"更多户型cell点击事件");
    
    if (_delegate != nil&&[_delegate respondsToSelector:@selector(didSelectHouseTypeWithLayoutId:)]) {
       MoreHouseTypeModel *model = [_models objectAtIndex:indexPath.row];
        [_delegate didSelectHouseTypeWithLayoutId:model.houseTypeId];
    }
}

#pragma mark - Private
- (void)loadMoreHouseTypePage{
   [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
       make.edges.equalTo(self);
   }];
}

#pragma mark Getter
-(UICollectionView *)collectionView{
    
    if (_collectionView == nil) {
        
        UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 200, 300) collectionViewLayout:flowLayout];
        _collectionView.dataSource = self;
        _collectionView.delegate   = self;
        _collectionView.backgroundColor = BackgroundColor240;
        [_collectionView registerClass:[MoreHouseTypeCollectionCell class] forCellWithReuseIdentifier:MoreHouseTypeCollectionCellIdentifier];
        [self addSubview:_collectionView];
    }
    return _collectionView;
}

#pragma mark - Setter
- (void)setModels:(NSMutableArray *)models{
    _models = models;
    [self.collectionView reloadData];
}
@end
